<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="客户端ip" prop="clientIp">
				<el-input v-model="queryParams.clientIp" placeholder="请输入登录地址" clearable style="width: 240px;" @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="用户名称" prop="createBy">
				<el-input v-model="queryParams.createBy" placeholder="请输入用户名称" clearable style="width: 240px;" @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="登录时间" style="width: 308px">
				<el-date-picker
					v-model="dateRange"
					value-format="YYYY-MM-DD HH:mm:ss"
					type="daterange"
					range-separator="-"
					start-placeholder="开始日期"
					end-placeholder="结束日期"
					:default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]"
					@change="handleQuery"
				></el-date-picker>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="handleQuery">搜索</el-button>
				<el-button @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="danger" icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['monitor:logininfor:remove']">删除</el-button>
			</el-col>
			<!-- <el-col :span="1.5">
				<el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['monitor:logininfor:export']">导出</el-button>
			</el-col> -->
			<right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table border ref="logininforRef" v-loading="loading" :data="logininforList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="50" align="center" />
			<el-table-column prop="startTime" label="登录时间" width="160">
				<template #default="scope">
					<span>{{ parseTime(scope.row.startTime) }}</span>
				</template>
			</el-table-column>
			<el-table-column prop="createBy" label="登录人员" width="150" show-overflow-tooltip  />
			<el-table-column prop="browser" label="浏览器" show-overflow-tooltip />
			<el-table-column prop="os" label="操作系统" show-overflow-tooltip />
			<el-table-column prop="clientIp" label="客户端ip" show-overflow-tooltip />
			<el-table-column prop="clientAddr" label="请求地址" show-overflow-tooltip />
			<el-table-column prop="userAgent" label="用户代理" show-overflow-tooltip />
		</el-table>

		<pagination v-show="total > 0" :total="total" v-model:page="queryParams.current" v-model:limit="queryParams.size" @pagination="getList" />
	</div>
</template>

<script setup name="Logininfor">
import { listLogininfor, delLogininfor} from "@/api/system/logininfor"

const { proxy } = getCurrentInstance()

const logininforList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const multiple = ref(true)
const total = ref(0)
const dateRange = ref([])

// 查询参数
const queryParams = ref({
  current: 1,
  size: 10,
	logType: 'LOGIN',
  clientIp: undefined,
  createBy: undefined
})

/** 查询登录日志列表 */
function getList() {
  loading.value = true
  listLogininfor(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
		const result = response.result || {}
    logininforList.value = result.records || []
    total.value = Number(result.total)
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.current = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = []
  proxy.resetForm("queryRef")
  queryParams.value.current = 1
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.logId)
  multiple.value = !selection.length
}

/** 删除按钮操作 */
function handleDelete(row) {
  const logIds = row.logId? [row.logId] : ids.value
	console.log(row, logIds)
  proxy.$modal.confirm('是否确认删除访问编号为"' + logIds + '"的数据项?').then(function () {
    return delLogininfor(logIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download("monitor/logininfor/export", {
    ...queryParams.value,
  }, `logininfor_${new Date().getTime()}.xlsx`)
}

getList()
</script>
